package ntd.util.frame.msg.service.handle.jpush.sms;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.jsms.api.template.SendTempSMSResult;
import cn.jsms.api.template.TempSMSResult;
import cn.jsms.api.template.TemplatePayload;
import ntd.util.frame.msg.MsgHandle;
import ntd.util.frame.msg.bo.MsgInBO;
import ntd.util.frame.msg.service.context.MsgContext;
import ntd.util.modular.msg.entity.SmsTemplate;

import java.util.Map;

import static ntd.util.frame.msg.service.handle.jpush.util.UtilJPush.transform;

public class JPushSmsHandle implements MsgHandle {

    @Override
    public Object handle(MsgContext msgContext) {
        if (msgContext.getAction().equals("sendMsg")) {
            return sendMsg(msgContext);
        }
        if (msgContext.getAction().equals("smsSync")) {
            smsSync(msgContext);
            return Boolean.TRUE;
        }
        return false;
    }

    private void smsSync(MsgContext msgContext) {
        JPushSmsHelper jPushSmsHelper = new JPushSmsHelper(transform(msgContext));
        for (SmsTemplate smsTemplate : msgContext.getSmsTemplates()) {
            String tempId = smsTemplate.getTempId();
            if (StrUtil.isNotBlank(tempId)) {
                TempSMSResult tempSMSResult = jPushSmsHelper.getSmsTemplate(smsTemplate.getTempId());
                if (tempSMSResult.isResultOK()) {
                    smsTemplate.setState(tempSMSResult.getStatus());
                    smsTemplate.setTemplate(tempSMSResult.getTemplate());
                    smsTemplate.setType(String.valueOf(tempSMSResult.getType()));
                    smsTemplate.setRemark(tempSMSResult.getRemark());
                    smsTemplate.setTtl(tempSMSResult.getRemark());
                }
            } else {
                TemplatePayload build = TemplatePayload.newBuilder()
                    .setRemark(smsTemplate.getRemark())
                    .setTemplate(smsTemplate.getTemplate())
                    .setTTL(Integer.parseInt(smsTemplate.getTtl()))
                    .setType(Integer.parseInt(smsTemplate.getType()))
                    .build();
                SendTempSMSResult sendTempSMSResult = jPushSmsHelper.saveSmsTemplate(build);
                if (sendTempSMSResult.isResultOK()) {
                    smsTemplate.setTempId(String.valueOf(sendTempSMSResult.getTempId()));
                }
            }
        }
    }

    private Boolean sendMsg(MsgContext msgContext) {
        JPushSmsHelper jPushSmsHelper = new JPushSmsHelper(transform(msgContext));

        jPushSmsHelper.sendSms(buildSmsInfo(msgContext));

        return Boolean.TRUE;
    }

    private JPushSmsHelper.JPushSmsInBO buildSmsInfo(MsgContext param) {
        JPushSmsHelper.JPushSmsInBO jPushSmsInBO = new JPushSmsHelper.JPushSmsInBO();

        MsgInBO msgInBO = param.getMsgInBO();
        JSONObject dataJson = JSONUtil.parseObj(msgInBO.getDataJson());

        jPushSmsInBO.setPhones(StrUtil.split(msgInBO.getReceiverPhones(), ","));
        jPushSmsInBO.setSmsTempId(dataJson.getInt("templateCode"));
        jPushSmsInBO.setSignId(dataJson.getInt("signId", -1));
        jPushSmsInBO.setParams(dataJson.get("templateParams", Map.class));

        return jPushSmsInBO;
    }
}
